package com.hopu.blb_erp.controller;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.StringUtils;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.hopu.blb_erp.entity.*;
import com.hopu.blb_erp.service.CustomerService;
import com.hopu.blb_erp.service.ISCompanyService;
import com.hopu.blb_erp.service.UserService;
import com.hopu.blb_erp.service.impl.SCompanyServiceImpl;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.ResponseEntity;
import org.springframework.security.core.context.SecurityContextHolder;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@Api
@RestController
public class CustomerController {
    @Autowired
    private CustomerService customerService;
    @Autowired
    private UserService userService;
    @Autowired
    private ISCompanyService companyService;

    @ApiOperation("分页查询客户")
    @GetMapping("/customers/page/{current}/{pageSize}")
    public ResponseEntity<Page<Customer>> findPage(@PathVariable("current")Integer current, @PathVariable("pageSize")Integer pageSize){
        Page<Customer> customerPage = new Page<>(current,pageSize);
        Page<Customer> page = customerService.page(customerPage);
        return ResponseEntity.ok(page);
    }
    @ApiOperation("查询所以客户")
    @GetMapping("/customers")
    public ResponseEntity<List<Customer>> findAll(){
        List<Customer> list =customerService.list();
        return ResponseEntity.ok(list);
    }
    @ApiOperation("添加客户及公司")
    @PostMapping("/customer")
    public ResponseEntity<String> addCustomer(@RequestBody SCompany company){
        customerService.saveCustomerCompany(company);
        return ResponseEntity.ok("ok");
    }

    @ApiOperation("更新客户")
    @PutMapping("/customer")
    public ResponseEntity<String> modifyCustomer(@RequestBody Customer customer){
        customerService.updateById(customer);
        return ResponseEntity.ok("ok");
    }

    @ApiOperation("删除客户")
    @DeleteMapping("/customer/{id}")
    public ResponseEntity<String> removeCustomer(@PathVariable("id")Integer id){
        customerService.removeById(id);
        return ResponseEntity.ok("ok");
    }

    @ApiOperation("模糊查询客户")
    @GetMapping("/customers/like")
    public ResponseEntity<PageEntity<Customer>> findLikeCustomer(Integer current, Integer pageSize, String province, String city, String region, String name){
        String address;
        if ("null".equals(name) || "".equals(name)){
            name = null;
        }
        if ("".equals(province) && "".equals(city) &&"".equals(region) ){
            address = null;
        }else {
            address = province + city + region;
        }
        org.springframework.security.core.userdetails.User user =
                (org.springframework.security.core.userdetails.User)
                        SecurityContextHolder.getContext().getAuthentication().getPrincipal();
        User byName = userService.findByName(user.getUsername());
        SCompany byId = companyService.getById(byName.getCompanyId());
        PageEntity<Customer> page = customerService.findCustomerPage(current, pageSize, name, address,byId.getCompanyName());

        return ResponseEntity.ok(page);
    }
}
